package org.lyq.erp.auth.menu.controller;

import java.util.List;
import javax.annotation.Resource;
import org.lyq.erp.auth.menu.pojo.Menu;
import org.lyq.erp.auth.menu.service.MenuService;
import org.lyq.erp.auth.role.pojo.Role;
import org.lyq.erp.auth.role.service.RoleService;
import org.lyq.erp.config.json.JSONResult;
import org.lyq.erp.utils.string.ObjectEmptyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping({ "/menu" })
@Controller
public class MenuController {
	
	private static Logger logger = LoggerFactory.getLogger(MenuController.class);

	@Resource
	private MenuService menuService;

	@Resource
	private RoleService roleService;

	@RequestMapping({ "/list" })
	public String list(ModelMap map) {
		getPageDates(map);
		return "menu_list";
	}

	@RequestMapping({ "/insertAndUpdate" })
	public String insertAndUpdate(Menu menu, Integer menUuid, ModelMap map) {
		if (!insertValidation(menu)) {
			menu.popuperrorMessage = "true";
			map.put("dataBean", menu);
			getPageDates(map);
			return "menu_list";
		}
		try {
			if (menu.getMenuUuid() == null) {
				menu.setMenuUuid(menu.getMenuUuidInsert());
				this.menuService.insert(menu);
			} else {
				this.menuService.update(menu);
			}
		} catch (Exception e) {

			logger.info(e.getMessage());
			menu.addErrorMessage("menu006");
			menu.popuperrorMessage = "true";
			map.put("dataBean", menu);
			getPageDates(map);
			return "menu_list";
		}
		return "redirect:/menu/list?menUuid=" + menUuid;
	}

	@RequestMapping({ "/getMenu" })
	@ResponseBody
	public JSONResult getMenu(Integer menuUuid) {
		if (menuUuid != null) {
			Menu menu = this.menuService.getMenuByMenuUuid(menuUuid);
			if (menu != null) {
				return JSONResult.ok(menu);
			}
			return JSONResult.errorMsg("menu005");
		}

		return JSONResult.errorMsg("menu005");
	}

	@RequestMapping({ "/delete" })
	public String delete(Menu menu, Integer menUuid, ModelMap map) {
		if (menu.getMenuUuid() != null) {
			try {
				this.menuService.delete(menu.getMenuUuid());
				return "redirect:/menu/list?menUuid=" + menUuid;
			} catch (Exception e) {
				logger.info(e.getMessage());
			}
		}
		menu.addErrorMessage("menu007");
		map.put("dataBean", menu);
		getPageDates(map);
		return "menu_list";
	}

	private void getPageDates(ModelMap map) {
		List<Menu> menuList = this.menuService.selectAllMenuAndPmenu();
		map.put("menuList", menuList);

		List<Role> rList = this.roleService.selectAllRole();
		map.addAttribute("rList", rList);

		List<Menu> PmenuList = this.menuService.selectAllPmenu();
		map.put("PmenuList", PmenuList);
	}

	private boolean insertValidation(Menu menu) {
		if (menu.getMenuUuid() == null && menu.getMenuUuidInsert() == null) {
			menu.addErrorMessage("menuUuidInsert", "menu001");
		}
		if (ObjectEmptyUtils.isEmptyString(menu.getMenuName())) {
			menu.addErrorMessage("menuName", "menu002");
		}
		if (ObjectEmptyUtils.isEmptyString(menu.getMenuUrl())) {
			menu.addErrorMessage("menuUrl", "menu003");
		}
		if (ObjectEmptyUtils.isEmptyString(menu.getMenuClasstype())) {
			menu.addErrorMessage("menuClasstype", "menu004");
		}

		return menu.getMap().isEmpty();
	}
}
